// pages/submitOrder/submitOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    no_pic:'/static/images/no-pic.png',
    gg_id:0,
    price:0,
    price_vip_zk:0,
    serverUrl:'',
    serverHeader:{},
    info:{address:{id:0},info:{},price_yj:0,wallet_left:0,wallet:0},
    sub:{wallet:0,gg_id:0,address:0,num:1,day_tip:'',day:'',time:'',note:'',yhq_id:0,yhq_price:0,city:'',imgs:[]},
    insufficient: false,
    show: false,
    couponShow:false,
    couponList:[],
    yhq_tip:'',
    show_wallet_tip:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      gg_id:options.gg_id,
      'sub.gg_id':options.gg_id,
      serverUrl:wx.util.interfaceUrl()+"upload",
      serverHeader:{
        'Deer-Device-Type':'wxapp',
        'Deer-Api-Version':wx.util.ver,
        'Deer-Api-Token': wx.util.getToken()
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(wx.util.fwDate.length==3){
      this.setData({
        'sub.day_tip':wx.util.fwDate[0],
        'sub.day':wx.util.fwDate[1],
        'sub.time':wx.util.fwDate[2],
      })
    }
    this.load();
  },
  async load(){
    wx.util.fwDate=[];
    let res=await wx.util.post("order_confrim/index",{gg_id:this.data.gg_id,day:this.data.sub.day,time:this.data.sub.time,num:this.data.sub.num});
    this.setData({
      info: res.data,
      price: res.data.price,
      price_vip_zk: res.data.price_vip_zk,
      'sub.address': res.data.address.id,
      couponList: res.data.coupon,
      yhq_tip: res.data.yhq_tip,
      'sub.yhq_id': 0,
      'sub.yhq_price': 0
    })
    this.rePrice();
    this.wallet_set({detail:{value:this.data.sub.wallet==1}})
  },
  toCharge(){
    wx.navigateTo({
      url:'/pages/wallet/wallet'
    })
  },
  wallet_set(e){
    this.setData({
      'sub.wallet': e.detail.value===true?1:0,
      show_wallet_tip: false
    })
    if(this.data.sub.wallet==1){
      this.setData({
        show_wallet_tip: this.data.info.wallet_left<this.data.price
      })
    }
  },
  setShow() {
    this.setData({
      couponShow:true
    })
  },
  result(e) {
    this.setData({
      'sub.imgs': e.detail.imgArr
    })
  },
  remove(e) {
    this.data.sub.imgs.splice(e.detail.index,1);
    this.setData({
      'sub.imgs': this.data.sub.imgs
    })
  },
  couponClose(){
    this.setData({
      couponShow: false
    })
  },
  couponConfirm(s){
    let e=s.detail;
    if(e.type=="no"){
      this.setData({
        yhq_tip: "不使用",
        'sub.yhq_id': 0,
        'sub.yhq_price': 0
      })
      this.rePrice();
      return;
    }
    this.setData({
      yhq_tip: "-"+e.price+'元',
      'sub.yhq_id': e.id,
      'sub.yhq_price': e.price
    })
    this.rePrice();
  },
  zxTime(){
    wx.navigateTo({
      url: "/pages/date/date?gg_id="+this.data.gg_id
    })
  },
  rePrice(){
    let price=parseFloat(this.data.info.info.price)*parseInt(this.data.sub.num)+parseFloat(this.data.info.price_yj)-parseFloat(this.data.sub.yhq_price)-parseFloat(this.data.price_vip_zk);
    price=price.toFixed(2)*1;
    this.setData({
      price: price
    })
  },
  change(e){
    this.setData({
      'sub.num': e.detail.value
    })
    this.rePrice();
    this.load();
  },
  chooseAddr() {
    wx.navigateTo({
      url: "/pages/address/address"
    })
  },
  async btnPay() {
    if(this.data.sub.address==0){
      wx.util.toast("请选择地址");
      return;
    }
    if(this.data.sub.day==''){
      wx.util.toast("请选择服务时间");
      return;
    }
    let city=wx.getStorageSync("city");
    if(city){
      this.setData({
        'sub.city': city
      })
    }

    let order=await wx.util.post("order/creat",this.data.sub);
    wx.setStorageSync("order_tab","-1");
    if(this.data.sub.wallet==1){
      wx.switchTab({
        url:"/pages/myOrder/myOrder"
      })
      return;
    }
    await this.pay(order.data.id);
  },
  async pay(id){
    let res=await wx.util.post("pay/weixin_jsapi",{id:id},false);
    wx.requestPayment({
      timeStamp: res.data.timeStamp,
      nonceStr: res.data.nonceStr,
      package: res.data.package,
      signType: res.data.signType,
      paySign: res.data.paySign,
      success (res) {
        wx.navigateTo({
          url: "/pages/success/success"
        })
      },
      fail (res) {
        wx.switchTab({
          url: "/pages/myOrder/myOrder"
        })
      }
      })
  },

})